<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="患者姓名" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="患者姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio v-model="dataForm.sex" :label="0">男</el-radio>
      <el-radio v-model="dataForm.sex" :label="1">女</el-radio>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="住址" prop="address">
      <el-input v-model="dataForm.address" placeholder="住址"></el-input>
    </el-form-item>
    <el-form-item label="诊断说明">
      <el-input v-model="dataForm.diagnosis" placeholder="诊断说明"></el-input>
    </el-form-item>
    <el-form-item label="影像类型">
      <el-checkbox-group v-model="dataForm.medicalImageTypeList">
        <el-checkbox :label="1">彩超</el-checkbox>
        <el-checkbox :label="2">CT</el-checkbox>
        <el-checkbox :label="3">B超</el-checkbox>
        <el-checkbox :label="4">尿检</el-checkbox>
        <el-checkbox :label="5">血常规</el-checkbox>
        <el-checkbox :label="6">核磁</el-checkbox>
        <el-checkbox :label="99">其他</el-checkbox>
      </el-checkbox-group>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="费用" prop="cost">
      <el-input v-model="dataForm.cost" placeholder="费用"></el-input>
    </el-form-item>
    <el-form-item label="reference">
      <el-autocomplete
        v-model="dataForm.recommender"
        :fetch-suggestions="querySearchAsync"
        placeholder="搜索reference"
        @select="handleSelect"
        value-key="recommenderName"
      ></el-autocomplete>
    </el-form-item>
    <el-form-item label="reference手机号">
      <el-input v-model="dataForm.recommenderMobile" placeholder="reference手机号"></el-input>
    </el-form-item>
    <el-form-item label="就诊时间">
      <el-date-picker
        v-model="dataForm.clinicTime"
        type="datetime"
        placeholder="选择日期时间"
        format="yyyy-MM-dd HH:mm:ss"
        value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="就诊次数" prop="visitCount">
      <el-input v-model="dataForm.visitCount" placeholder="就诊次数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        restaurants: [],
        state: '',
        timeout:  null,
        recommenderId: 0,
        dataForm: {
          id: 0,
          patientName: '',
          sex: '',
          age: '',
          address: '',
          diagnosis: '',
          medicalImageType: '',
          mobile: '',
          cost: '',
          recommender: '',
          recommenderMobile: '',
          visitCount: '',
          medicalImageTypeList: [],
          clinicTime: ''
        },
        dataRule: {
          patientName: [
            { required: true, message: '患者姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '住址不能为空', trigger: 'blur' }
          ],
          diagnosis: [
            { required: true, message: '诊断说明不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          cost: [
            { required: true, message: '费用不能为空', trigger: 'blur' }
          ],
          recommender: [
            { required: true, message: '推荐人不能为空', trigger: 'blur' }
          ],
          recommenderMobile: [
            { required: true, message: '推荐人联系方式不能为空', trigger: 'blur' }
          ],
          visitCount: [
            { required: true, message: '就诊次数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.restaurants = this.loadAll();
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/patientvisitrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.recommenderId = data.sysPatientVisitRecord.recommenderId
                this.dataForm.patientName = data.sysPatientVisitRecord.patientName
                this.dataForm.sex = data.sysPatientVisitRecord.sex
                this.dataForm.age = data.sysPatientVisitRecord.age
                this.dataForm.address = data.sysPatientVisitRecord.address
                this.dataForm.diagnosis = data.sysPatientVisitRecord.diagnosis
                this.dataForm.medicalImageType = data.sysPatientVisitRecord.medicalImageType
                this.dataForm.mobile = data.sysPatientVisitRecord.mobile
                this.dataForm.cost = data.sysPatientVisitRecord.cost
                this.dataForm.recommender = data.sysPatientVisitRecord.recommender
                this.dataForm.recommenderMobile = data.sysPatientVisitRecord.recommenderMobile
                this.dataForm.visitCount = data.sysPatientVisitRecord.visitCount
                this.dataForm.medicalImageTypeList = data.sysPatientVisitRecord.medicalImageTypeList
                this.dataForm.clinicTime = data.sysPatientVisitRecord.clinicTime
              }
            })
          }
        })
      },
      loadAll() {
         this.$http({
          url: this.$http.adornUrl(`/sys/sysrecommender/search`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.restaurants = data.data
          }
        })
      },
      querySearchAsync(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;


        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 3000 * Math.random());
      },
      createStateFilter(queryString) {
        return (state) => {
          return (state.recommenderName.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      handleSelect(item) {
        this.recommenderId = item.id
        this.dataForm.recommenderMobile = item.recommenderMobile
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/patientvisitrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'patientName': this.dataForm.patientName,
                'sex': this.dataForm.sex,
                'age': this.dataForm.age,
                'address': this.dataForm.address,
                'diagnosis': this.dataForm.diagnosis,
                'mobile': this.dataForm.mobile,
                'cost': this.dataForm.cost,
                'visitCount': this.dataForm.visitCount,
                medicalImageTypeList: this.dataForm.medicalImageTypeList,
                clinicTime: this.dataForm.clinicTime,
                recommenderId: this.recommenderId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
